package movee.zookeeper.leader.selector.cluster;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author movee
 */
@Slf4j
public class SimpleClusterEventListenerRegistry<T extends ClusterEventListener>
        implements ClusterEventListenerRegistry<T> {

    private Map<String, T> listeners = new HashMap<>();

    @Override
    public void registerClusterEventListener(final String name, final T listener) {
        if (listener != null && !StringUtils.isEmpty(name)) {
            listeners.put(name, listener);
        }
    }

    @Override
    public T removeClusterEventListener(final String name) {
        return listeners.remove(name);
    }

    @Override
    public Boolean containClusterEventListener(final String name) {
        return listeners.containsKey(name);
    }

    @Override
    public List<T> getListeners() {
        return new ArrayList<>(listeners.values());
    }

    @Override
    public Iterator<T> iterator() {
        return listeners.values().iterator();
    }

    @Override
    public List<String> getListenerNames() {
        return new ArrayList<>(listeners.keySet());
    }

    @Override
    public T getListener(String name) {
        return listeners.get(name);
    }
}
